package com.hhq.demo.wechat.controller;

import com.hhq.demo.bean.Courier;
import com.hhq.demo.bean.User;
import com.hhq.demo.mvc.ResponseBody;
import com.hhq.demo.service.CourierService;
import com.hhq.demo.service.ExpressService;
import com.hhq.demo.service.UserService;
import com.hhq.demo.service.impl.CourierServiceImpl;
import com.hhq.demo.service.impl.ExpressServiceImpl;
import com.hhq.demo.service.impl.UserServiceImpl;
import com.hhq.demo.utils.JSONUtil;
import com.hhq.demo.utils.Message;
import com.hhq.demo.utils.RandomUtil;
import com.hhq.demo.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author HHQ
 * @date 2021年12月17日
 */
public class UserController {

    CourierService courierService = new CourierServiceImpl();
    UserService userService = new UserServiceImpl();

    @ResponseBody("/wechat/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String code = RandomUtil.getCode();
        //短信验证码发送，获取发送结果
        System.out.println(userPhone+"的登录验证码为："+code);
        //此处默认发送成功
        boolean flag = true;
        Message message = new Message();
        if (flag) {
            message.setStatus(1);
            message.setResult("验证码已发送，请注意查收！");
        } else {
            message.setStatus(0);
            message.setResult("验证码发送失败，请检查手机号稍后再试！");
        }
        UserUtil.setUserSms(request.getSession(),userPhone,code);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/sendCheckedVerifyCode.do")
    public String sendCheckedVerifyCode(HttpServletRequest request, HttpServletResponse response){
        User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
        String phone = request.getParameter("phone");
        if (!wxUserInfo.getPhone().equals(phone)){
            return JSONUtil.toJSON(new Message(0,"手机号不一致，短信获取失败"));
        }
        String code = RandomUtil.getCode();
        //短信验证码发送，获取发送结果
        //flag = SMSUtil.loginSMS(userPhone,code)
        System.out.println(wxUserInfo.getPhone()+"的修改验证码为："+code);
        //此处默认发送成功
        boolean flag = true;
        Message message = new Message();
        if (flag) {
            message.setStatus(1);
            message.setResult("验证码已发送，请注意查收！");
        } else {
            message.setStatus(0);
            message.setResult("验证码发送失败，请检查手机号稍后再试！");
        }
        UserUtil.setUserSms(request.getSession(),wxUserInfo.getPhone(),code);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String code = request.getParameter("code");
        String userSms = UserUtil.getUserSms(request.getSession(), userPhone);
        Message message = new Message();
        //判断手机号与验证码是否对应准确
        if (userSms==null){
            message.setStatus(-1);
            message.setResult("请检查手机号码是否正确！");
        } else if (userSms.equals(code)){
            //登录成功
            //判断是快递员还是用户
            //从用户表中查询是否存在该用户
            User user = userService.findByPhone(userPhone);
            if (user.getId()==null) {
                //该用户不存在，则注册该用户
                user.setUsername("用户"+RandomUtil.getCode());
                user.setPassword("123456");
                user.setLoginTime(new Date());
                user.setPhone(userPhone);
                user.setRegisterTime(new Date());
                userService.insert(user);
            } else {
                //修改登录时间
                userService.updateLoginTimeById(user.getId());
            }
            //从快递员表中查询是否有对应号码
            Courier courierPhone = courierService.findByPhone(userPhone);
            if (userPhone.equals(courierPhone.getPhone())){
                //快递员
                message.setStatus(1);
                user.setUser(false);
            } else {
                //用户
                message.setStatus(0);
                user.setUser(true);
            }
            UserUtil.setWxUser(request.getSession(),user);
            message.setResult("登录成功");
            //修改登录时间

        } else {
            //验证码不一致
            message.setStatus(-2);
            message.setResult("验证码错误");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().invalidate();
        return JSONUtil.toJSON(new Message(1));
    }

    @ResponseBody("/wechat/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
        boolean isUser = wxUserInfo.isUser();
        Message message = new Message();
        if (isUser) {
            message.setStatus(0);
        } else {
            message.setStatus(1);
        }
        message.setResult(wxUserInfo.getPhone());
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/getUser.do")
    public String getUsername(HttpServletRequest request, HttpServletResponse response){
        return JSONUtil.toJSON(UserUtil.getWxUserInfo(request.getSession()));
    }


    @ResponseBody("/wechat/checkStarUserInfo.do")
    public String checkStarUserInfo(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String userSms = UserUtil.getUserSms(request.getSession(), phone);
        Message message = new Message();
        //判断手机号与验证码是否对应准确
        if (userSms==null){
            message.setStatus(-1);
            message.setResult("请先获取验证码！");
        } else if (userSms.equals(code)){
            //认证成功
            User user = new User(username, password, phone);
            int i = userService.updateByPhone(user);
            if (i>0) {
                message.setStatus(1);
                message.setResult("认证成功");
                UserUtil.setWxUser(request.getSession(),userService.findByPhone(phone));
            } else {
                message.setStatus(0);
                message.setResult("认证失败");
            }
        } else {
            //验证码不一致
            message.setStatus(-2);
            message.setResult("验证码错误");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/updateAndPhone.do")
    public String updateAndPhone(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String code = request.getParameter("code");
        String phone = request.getParameter("phone");
        User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
        String userSms = UserUtil.getUserSms(request.getSession(), phone);
        Message message = new Message();
        if (userSms==null){
            message.setStatus(-1);
            message.setResult("当前号码未获取验证码");
        } else if (userSms.equals(code)){
            //认证成功
            int i = userService.updateUsername(phone,username);
            if (i>0) {
                message.setStatus(1);
                message.setResult("修改成功");
                //修改快递员表中的信息
                courierService.updateUsername(phone,username);
                UserUtil.setWxUser(request.getSession(),userService.findByPhone(phone));
            } else {
                message.setStatus(0);
                message.setResult("修改失败");
            }
        } else {
            //验证码不一致
            message.setStatus(-2);
            message.setResult("验证码错误");
        }
        return JSONUtil.toJSON(message);
    }

}
